#include "systemtrayicondemo.h"
#include <QAction>
#include <QWidgetAction>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLabel>
#include <QMenu>
#include <QDebug>
#include <QDesktopServices>

/**
 * 参考：http://blog.csdn.net/taiyang1987912/article/details/46965171
 *
 */
SystemTrayIconDemo::SystemTrayIconDemo(QObject *parent) :
    QSystemTrayIcon(parent)
{
    qDebug()<<"SystemTray avaiable:"<<QSystemTrayIcon::isSystemTrayAvailable();
    /**
     * create Actions
    */
    QMenu *pop_menu=new QMenu();
    pop_menu->setTitle("Title");

    //普通Action
    QAction * show_action=new QAction(pop_menu);
    QAction *help_action=new QAction(pop_menu);
    connect(show_action,SIGNAL(triggered()),this,SLOT(showWidget()));
    connect(help_action,SIGNAL(triggered()),this,SLOT(help()));


    //QWidgetAction
    QWidget * play_widget=new QWidget();    
    QWidgetAction* play_widget_action=new QWidgetAction(pop_menu);

    QLabel* music_name_label=new QLabel();
    music_name_label->setFixedWidth(100);
    music_name_label->setAlignment(Qt::AlignCenter);

    QPushButton* play_button=new QPushButton();
    QPixmap play_pixmap(":/drawable/action/play.jpg");
    play_button->setIcon(QPixmap(play_pixmap));
    play_button->setIconSize(QSize(40,40));
    play_button->setObjectName("playButton");

    QPushButton* stop_button=new QPushButton();
    QPixmap stop_pixmap(":/drawable/action/Stop.jpg");
    stop_button->setIcon(QPixmap(stop_pixmap));
    stop_button->setIconSize(QSize(40,40));
    stop_button->setObjectName("stopButton");

    QHBoxLayout* layout=new QHBoxLayout();
    layout->addWidget(play_button);
    layout->addWidget(stop_button);
    layout->setSpacing(1);
    layout->setContentsMargins(20,0,20,0);

    QVBoxLayout* layout2=new QVBoxLayout();
    layout2->addLayout(layout);
    layout2->addWidget(music_name_label,0,Qt::AlignCenter);
    layout2->setSpacing(5);
    layout2->setContentsMargins(0,0,0,5);
    music_name_label->setText("Player");
    music_name_label->setEnabled(false);

    play_widget->setLayout(layout2);
    play_widget_action->setDefaultWidget(play_widget);

    connect(play_button,SIGNAL(clicked()),this,SLOT(play()));
    connect(stop_button,SIGNAL(clicked()),this,SLOT(stop()));

    /**
    * translateLanguage
    */
    setToolTip(tr("hey music"));
    //switchPlayToolTip()
    play_button->setToolTip("play");
    stop_button->setToolTip("Stop");
    show_action->setText("show");
    help_action->setText("help");
    help_action->setIcon(QIcon(":/drawable/action/ic_launcher.png"));
    /**
     * add Actions
     */
    pop_menu->addAction(play_widget_action);
    pop_menu->addSeparator();

    pop_menu->addAction(show_action);
    pop_menu->addSeparator();

    pop_menu->addAction(help_action);

    /**
     * 调用setContextMenu
     */
    setIcon(QIcon(":/drawable/action/ic_launcher.png"));
    setContextMenu(pop_menu);

    //点击时
    connect(this,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(Activiated(QSystemTrayIcon::ActivationReason)));

}

void SystemTrayIconDemo::play()
{

}
void SystemTrayIconDemo::stop()
{

}
void SystemTrayIconDemo::showWidget()
{

}
void SystemTrayIconDemo::Activiated(QSystemTrayIcon::ActivationReason reason)
{
    qDebug()<<reason;
    switch(reason){
        case QSystemTrayIcon::Trigger:
        case QSystemTrayIcon::DoubleClick: //双击或者单击
        break;
        case QSystemTrayIcon::Context:
        //右键
        break;
    case QSystemTrayIcon::MiddleClick://中键
        break;
    }
}
#include <QUrl>
void SystemTrayIconDemo::help()
{
    QDesktopServices::openUrl(QUrl("http://www.baidu.com"));
}
